<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { ElButton, ElTag } from 'element-plus'
import { Table } from '@/components/Table'
import { ucUserListAdminApi } from '@/api/uc/user'
import { useTable } from '@/hooks/web/useTableTest'
import { TableData } from '@/api/uc/user/types'
import { h, reactive, ref } from 'vue'
// import { ucOrgListUrlApi } from '@/api/forms/index'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { dbUserListApi } from '@/api/forms'
// 获取字典
// import { useCache } from '@/hooks/web/useCache'
// const { wsCache } = useCache()
const { register, tableObject, methods } = useTable<
  {
    total: number
    list: TableData[]
  },
  TableData
>({
  getListApi: ucUserListAdminApi,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { setSearchParams } = methods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'user_id',
    label: '主键',
    fixed: 'left',
    width: 60,
    table: {
      show: true
    },
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'username',
    label: '用户名',
    search: {
      show: true
    }
  },
  {
    field: 'nickname',
    label: '姓名',
    search: {
      show: true
    }
  },
  {
    field: 'email',
    label: '电子邮箱',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'mobile',
    label: '手机号',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'is_stop',
    label: '是否停用',
    width: 100,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return h(
        ElTag,
        {
          type: cellValue == '0' ? 'success' : cellValue == '1' ? 'warning' : 'danger'
        },
        () => (cellValue == '0' ? '启用' : cellValue == '1' ? '停用' : '未知')
      )
    },
    form: {
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '启用',
            value: '0'
          },
          {
            label: '停用',
            value: '1'
          }
        ]
      }
    }
  },
  {
    field: 'create_time',
    label: '创建时间',
    width: 170,
    form: {
      show: false,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  },
  {
    field: 'update_time',
    label: '修改时间',
    width: 170,
    form: {
      show: false,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const exportClick = async () => {
  // 弹出导出框
  dialogTableVisible.value = true
  // 获取查询条件
  let aaa: any = tableObject.paramsObj.params
  console.log(aaa)
  // 获取条数
  let { data }: any = await dbUserListApi({
    email: aaa.email,
    mobile: aaa.mobile,
    nickname: aaa.nickname,
    // pageNo: 1,
    // pageSize: 10,
    username: aaa.username
  })
    .catch(() => {})
    .finally(() => {})
  if (data) {
    FormsNumber.value = Math.ceil(data.count / 10000)
  }
}

// 下载次数
const FormsNumber = ref(0)
const dialogTableVisible = ref(false)

const Export = async (index) => {
  let aaa: any = tableObject.paramsObj.params
  let { data }: any = await dbUserListApi({
    email: aaa.email,
    mobile: aaa.mobile,
    nickname: aaa.nickname,
    username: aaa.username,
    pageNo: index,
    pageSize: 10000
  })
    .catch(() => {})
    .finally(() => {
      // saveLoading.value = false
    })
  if (data) {
    window.open(data.url)
  }
}
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
    <div class="mb-10px w-[100%] flex justify-end" v-if="tableObject.paramsObj.params">
      <ElButton type="primary" @click="exportClick()"> 导出EXCEL </ElButton>
    </div>
    <Table
      id="my-table"
      v-show="tableObject.paramsObj.params"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      :selection="false"
      @register="register"
    />
  </ContentWrap>

  <el-dialog v-model="dialogTableVisible" title="用户主档导出详情">
    <div style="margin: 20px" v-for="(item, index) in FormsNumber" :key="index">
      <span style="margin: 20px">用户主档 - {{ item }}</span>
      <ElButton type="primary" @click="Export(item)">导出</ElButton>
    </div>
  </el-dialog>
</template>
